﻿<Window x:Class="_12.BindToList.SortGroupInXaml"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:_12.BindToList"
        xmlns:p11="clr-namespace:_11.DataBinding;assembly=11.DataBinding"
        xmlns:compModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
        xmlns:data="clr-namespace:System.Windows.Data;assembly=PresentationFramework"
        Title="SortGroupInXaml" Height="300" Width="300">
    <Window.Resources>
        <local:PersonCollection x:Key="persons">
            <p11:Person Age="10" Name="Henry"/>
            <p11:Person Age="40" Name="Mary"/>
            <p11:Person Age="40" Name="Mary's Husband"/>
            <p11:Person Age="70" Name="Alice"/>
            <p11:Person Age="60" Name="Dick"/>
            <p11:Person Age="60" Name="Dick's Wife"/>
        </local:PersonCollection>
        <local:AgeRanger x:Key="ageRanger"/>
        <Style TargetType="Button">
            <Setter Property="Margin" Value="5"/>
            <Setter Property="Background" Value="LightGreen"/>
        </Style>
        <GroupStyle x:Key="customGrpStyle">
            <GroupStyle.HeaderTemplate>
                <DataTemplate>
                    <TextBlock Background="Black" Foreground="White" FontWeight="Bold">
                        <TextBlock Text="{Binding Path=Name}"/>
                        (<TextBlock Text="{Binding Path=ItemCount}"/>)
                    </TextBlock>
                </DataTemplate>
            </GroupStyle.HeaderTemplate>
        </GroupStyle>
        <CollectionViewSource x:Key="sortedGrpedView" Source="{StaticResource persons}">
            <!--note: both 'SortDescriptions' and 'GroupDescriptions' are collections
            you can add multiple items into it-->
            <CollectionViewSource.SortDescriptions>
                <compModel:SortDescription PropertyName="Name"/>
            </CollectionViewSource.SortDescriptions>
            <CollectionViewSource.GroupDescriptions>
                <data:PropertyGroupDescription PropertyName="Age" 
                                               Converter="{StaticResource ageRanger}"/>
            </CollectionViewSource.GroupDescriptions>
        </CollectionViewSource>
    </Window.Resources>
    <Grid>
        <!--note: not bind to the original data source, but bind to the one
        which has been sorted and ordered-->
        <ListBox  ItemsSource="{Binding Source={StaticResource sortedGrpedView}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <TextBlock Text="{Binding Path=Name}"/>
                        (age:
                        <TextBlock Text="{Binding Path=Age}"/>)
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Background="Black" Foreground="White" FontWeight="Bold"> 
                                <TextBlock Text="{Binding Path=Name}"/>
                                (<TextBlock Text="{Binding Path=ItemCount}"/>)
                            </TextBlock>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListBox.GroupStyle>
        </ListBox>
    </Grid>
</Window>
